package com.qdlc.p2p.biz.service.user.identify;

import java.io.File;

import com.qdlc.p2p.biz.service.UserCacheService;
import com.qdlc.p2p.biz.service.UserIdentifyService;
import com.qdlc.p2p.biz.util.HclientFileUtil;
import com.qdlc.p2p.common.util.BeanUtil;
import com.qdlc.p2p.common.util.UploadFileUtil;
import com.qdlc.p2p.dal.dto.UserIdentify;
import com.qdlc.p2p.dal.model.UserModel;

/**
 * 手动审核方式
 * 
 * @author sj
 * @version 2.0
 * @since 2014年3月21日
 */
public class ManualUserIdentifyWay extends BaseUserIdentifyWay {
	
	private UserCacheService userCacheService;
	private UserIdentifyService userIdentifyService;
	private String sep = File.separator;

	private long userId;
	private UserModel model;

	public ManualUserIdentifyWay(long userId, UserModel model) {
		this.userId = userId;
		this.model = model;
	}

	@Override
	public Object doRealname() throws Exception {
		userCacheService = (UserCacheService) BeanUtil.getBean("userCacheService");
		userIdentifyService = (UserIdentifyService) BeanUtil.getBean("userIdentifyService");
		// 验证上传的文件
		model.validAttestationCommit(model.getCardPositive());
		model.validAttestationCommit(model.getCardOpposite());
		
		// 上传图片并保存图片路径
		String filePath1 = getFilePath(userId, sep, model.getCardPositive());
		String filePath2 = getFilePath(userId, sep, model.getCardOpposite());
		
		userCacheService.modify(userId, model, filePath1, filePath2);
		
		UserIdentify userIdentify = userIdentifyService.findByUserId(userId);
		userIdentifyService.modifyRealnameStatus(userId, 2, userIdentify.getRealNameStatus());
		return null;
	}
	
	public String getFilePath(long userId, String sep, File file) throws Exception {
		//读取配置文件中 身份证上传的配置进行 httpClient 请求
		return HclientFileUtil.uploadFileMethod(UploadFileUtil.getMessage("cardUrl"), UploadFileUtil.getMessage("cardNid"), file);
		/*Date d1 = DateUtil.getNow();
		String upfiesDir = ServletActionContext.getServletContext().getRealPath("/data/upfiles/images/");
		String realPath = ServletActionContext.getServletContext().getRealPath("");
		String destFileName = upfiesDir + DateUtil.dateStr2(d1) + sep + userId + "_attestation" + "_"
				+ d1.getTime() + ".jpg";
		File imageFile1 = null;
		imageFile1 = new File(destFileName);
		FileUtils.copyFile(file, imageFile1);
		logger.error("文件："+imageFile1.getPath());
		String filePath = destFileName.replace(realPath, "");
		return filePath.replace(sep, "/");*/
	}

	@SuppressWarnings("unused")
	private String truncatUrl(String old, String truncat, String sep) {
		String url = "";
		url = old.replace(truncat, "");
		url = url.replace(sep, "/");
		return url;
	}

}
